import { Request, Response } from 'express';
import { getToken } from '../src/utils/utils';

const waitTime = (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

async function getFakeCaptcha(req: Request, res: Response) {
  await waitTime(2000);
  return res.json('captcha-xxx');
}

const { ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION } = process.env;

/**
 * 当前用户的权限，如果为空代表没登录
 * current user access， if is '', user need login
 * 如果是 pro 的预览，默认是有权限的
 */
let access = ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site' ? 'admin' : '';

const getAccess = () => {
  return access;
};

// 代码中会兼容本地 service mock 以及部署站点的静态数据
export default {
  // 支持值为 Object 和 Array
  'GET /api/getCurrentUserInfo': (req: Request, res: Response) => {
    if (getToken() !== 'token' && getToken() !== undefined) {
      res.send({
        code: 10000,
        message: '成功',
        data: {
          name: 'Serati Ma',
        },
      });
    } else {
      res.send({
        code: -1,
        message: '请先登录！',
      });
    }
  },

  'GET /api/loginManage/getImageCode': (req: Request, res: Response) => {
    res.send({
      code: 10000,
      message: '成功',
      data: {
        imageCodeKey: '990eb89f-81ef-448f-97e3-959db399b75e',
        imageCodeBase64:
          'data:image/png;BASE64,iVBORw0KGgoAAAANSUhEUgAAAFAAAAAYCAIAAADieO37AAAESUlEQVR4Xu2WX2hbVRzH8yI+ycBHH33yQcYYTiZsgoKiMhCGshV80MlAmaKIpRtrwTGdQ7bVwSy6tEvauCR1y0o765r+uUvTdV1K/6U1SdPc3C1JG/PnJk1ukpvc/PUXb3I99yS5SVynRf3wezj3e84JfHLPuefICv8xZHjwb+d/4X8I0v64UHhfBavOZ4TC++qxXYRRBHMVuwmFd4tB5Rvx347CGI1oNw4uzFlcvvcusIQZyyWIxDaJBb2W6NNM9E4ujceTMXxEA9Br3Gir332XxTvKCNqynq/55P44M3CI6tlt/fElu/ELL0tnRBNqIBLORROBT773vXu+cWHQ++m2amj6OuUlnV7HDaM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dYIozw68y1x3nrhbc7vJ5gr0gzqsG8AAAAASUVORK5CYII=',
      },
    });
  },

  'POST /api/loginManage/userLogin': async (req: Request, res: Response) => {
    await waitTime(2000);
    res.send({
      code: 10000,
      message: '成功',
      data: {
        token:
          'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MzUxMjYyNDMsImFpZCI6MX0.-JMBwU_yzpJmgVBEstUx0qYXjXb5bVqUef4asYqUOY0',
        name: 'admin',
      },
    });
    return;
  },
  'GET /api/login/outLogin': (req: Request, res: Response) => {
    access = '';
    res.send({ data: {}, success: true });
  },
  'POST /api/register': (req: Request, res: Response) => {
    res.send({ status: 'ok', currentAuthority: 'user', success: true });
  },
  'GET /api/500': (req: Request, res: Response) => {
    res.status(500).send({
      timestamp: 1513932555104,
      status: 500,
      error: 'error',
      message: 'error',
      path: '/base/category/list',
    });
  },
  'GET /api/404': (req: Request, res: Response) => {
    res.status(404).send({
      timestamp: 1513932643431,
      status: 404,
      error: 'Not Found',
      message: 'No message available',
      path: '/base/category/list/2121212',
    });
  },
  'GET /api/403': (req: Request, res: Response) => {
    res.status(403).send({
      timestamp: 1513932555104,
      status: 403,
      error: 'Unauthorized',
      message: 'Unauthorized',
      path: '/base/category/list',
    });
  },
  'GET /api/401': (req: Request, res: Response) => {
    res.status(401).send({
      timestamp: 1513932555104,
      status: 401,
      error: 'Unauthorized',
      message: 'Unauthorized',
      path: '/base/category/list',
    });
  },

  'GET  /api/login/captcha': getFakeCaptcha,

  // 图书管理
  'GET /api/bookManage/getBookList': (req: Request, res: Response) => {
    res.send({
      code: 10000,
      message: '成功',
      data: {
        rowNumber: '6',
        totalNumber: '14043',
        list: [
          {
            aid: '1',
            isbn: '978-7-1115-5807-1',
            typeName: '计算机',
            name: '计算机组成原理',
            quantity: 2,
            createTime: '2021-10-18 09:31:48',
          },
          {
            aid: '2',
            isbn: '978-7-1112-4281-9',
            typeName: 'JAVA开发',
            name: 'Java Web整合开发完全自学手册',
            quantity: 0,
            createTime: '2021-10-18 09:33:24',
          },
          {
            aid: '3',
            isbn: '978-7-1112-4281-2',
            typeName: '管理学',
            name: '数据结构',
            quantity: 1,
            createTime: '2021-10-21 10:43:32',
          },
          {
            aid: '4',
            isbn: '978-7-1112-4281-3',
            typeName: 'JAVA开发',
            name: 'web前端',
            quantity: 2,
            createTime: '2021-10-21 10:43:33',
          },
          {
            aid: '5',
            isbn: '978-7-1112-4281-4',
            typeName: '网页设计',
            name: 'springboot',
            quantity: 3,
            createTime: '2021-10-21 10:43:35',
          },
          {
            aid: '6',
            isbn: '978-7-1112-4281-5',
            typeName: 'MySQL',
            name: 'mybatis',
            quantity: 4,
            createTime: '2021-10-21 10:43:36',
          },
        ],
      },
    });
  },

  'GET /api/bookManage/getBookType': (req: Request, res: Response) => {
    res.send({
      code: 10001,
      message: '成功',
      data: [
        { aid: '1', name: '计算机' },
        { aid: '2', name: '管理学' },
        { aid: '3', name: 'JAVA开发' },
        { aid: '4', name: '网页设计' },
        { aid: '5', name: 'MySQL' },
        { aid: '6', name: 'Javascript' },
        { aid: '7', name: '面对对象程序设计' },
      ],
    });
  },

  // 用户管理
  'GET /api/userManage/getUserList': (req: Request, res: Response) => {
    res.send({
      code: 10002,
      message: '成功',
      data: {
        rowNumber: '6',
        list: [
          {
            aid: 1,
            name: 'admin',
            phone: '12345678910',
            password: 'abc123',
            createTime: '2021-10-18 09:17:05',
            stateId: 1,
          },
          {
            aid: 3,
            name: 'test1',
            phone: '13333333333',
            password: 'abc123',
            createTime: '2021-10-20 13:57:37',
            stateId: 0,
          },
          {
            aid: 4,
            name: 'test2',
            phone: '14444444444',
            password: 'abc123',
            createTime: '2021-10-20 13:58:23',
            stateId: 1,
          },
          {
            aid: 5,
            name: 'test3',
            phone: '15555555555',
            password: 'abc123',
            createTime: '2021-10-20 13:58:55',
            stateId: 1,
          },
          {
            aid: 6,
            name: 'test4',
            phone: '16666666666',
            password: 'abc123',
            createTime: '2021-10-20 13:59:19',
            stateId: 1,
          },
        ],
      },
    });
  },

  'GET /api/userManage/getUserStateList': (req: Request, res: Response) => {
    res.send({
      code: 10003,
      message: '成功',
      data: [
        { id: '0', name: '禁用' },
        { id: '1', name: '启用' },
      ],
    });
  },

  // 分类管理
  'GET /api/classificationManage/getClassificationList': (req: Request, res: Response) => {
    res.send({
      code: 10004,
      message: '成功',
      data: {
        rowNumber: '6',
        list: [
          {
            aid: 1,
            name: '计算机',
            isDelete: 1,
            children: [
              {
                aid: 3,
                name: 'JAVA开发',
                isDelete: 1,
                children: [
                  {
                    aid: 8,
                    name: 'Java Web整合开发完全自学手册',
                    isDelete: 0,
                  },
                ],
              },
              {
                aid: 4,
                name: '网页设计',
                isDelete: 0,
                children: [],
              },
              {
                aid: 5,
                name: 'MySQL',
                isDelete: 1,
                children: [],
              },
              {
                aid: 6,
                name: 'Javascript',
                isDelete: 0,
                children: [],
              },
              {
                aid: 7,
                name: '面对对象程序设计',
                isDelete: 1,
                children: [],
              },
            ],
          },
          {
            aid: 2,
            name: '管理学',
            isDelete: 0,
            children: [
              {
                aid: 8,
                name: '管理者必读12篇',
                isDelete: 1,
                children: [],
              },
              {
                aid: 9,
                name: '管理的实践',
                isDelete: 1,
                children: [],
              },
              {
                aid: 10,
                name: '领导力21法则',
                isDelete: 1,
                children: [],
              },
              {
                aid: 11,
                name: '基业长青：理想公司的成功理念',
                isDelete: 1,
                children: [],
              },
            ],
          },
        ],
      },
    });
  },

  'GET /api/classificationManage/getTypeList': (req: Request, res: Response) => {
    res.send({
      code: 10005,
      message: '成功',
      data: [
        {
          aid: 1,
          name: '计算机',
          children: [
            {
              aid: 3,
              name: 'JAVA开发',
            },
            {
              aid: 4,
              name: '网页设计',
              children: [],
            },
            {
              aid: 5,
              name: 'MySQL',
              children: [],
            },
            {
              aid: 6,
              name: 'Javascript',
              children: [],
            },
            {
              aid: 7,
              name: '面对对象程序设计',
              children: [],
            },
          ],
        },
        {
          aid: 2,
          name: '管理学',
          children: [],
        },
      ]
    });
  },
};
